﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskFramework
{
    internal delegate void BatchScopeEndedHandler(int batchId);

    public class BatchScope : IDisposable
    {
        private readonly BatchScopeEndedHandler _onBatchScopeEnded;
        private static readonly object _lock = typeof(BatchScope);
        private static int _batchId = 1;

        internal BatchScope(BatchScopeEndedHandler onBatchScopeEnded)
        {
            if (onBatchScopeEnded == null)
            {
                throw new ArgumentNullException("onBatchScopeEnded");
            }

            _onBatchScopeEnded = onBatchScopeEnded;

            lock (_lock)
            {
                BatchId = _batchId++;
            }
        }

        public void Dispose()
        {
            _onBatchScopeEnded.Invoke(_batchId);
        }

        public int BatchId { get; private set; }
    }
}
